package com.sy.controller;

import com.sy.annocation.CurUser;
import com.sy.entity.Admin;
import com.sy.entity.User;
import com.sy.exception.BusinessException;
import com.sy.service.UserService;
import com.sy.util.CookieOperations;
import com.sy.util.RandomUtils;
import com.sy.util.RedisOperations;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

import static com.sy.constant.BusinessConstant.*;

/**
 * @Author:cxl
 * @Date: 2021/10/12 12:36
 */


@RestController
//@Api(tags = "这是用户的相关模块")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private CookieOperations cookieOperations;

    @Autowired
    private RedisOperations redisOperations;


    /**
     * @Author:cxl
     * 确认登陆用户是否存在
     * @param userName
     * @return
     */
//    @ApiOperation(value = "用户操作", notes = "确认登陆用户是否存在")
    @RequestMapping(value = "user",method = RequestMethod.GET,params = {"userName"})
    public boolean isUserNameExists(@RequestParam("userName") String userName){
        return  userService.isUserNameExists(userName);
    }

    /**
     * @Author:cxl
     * 登陆操作
     * @param user
     * @return
     */
    @RequestMapping(value = "login", method = RequestMethod.POST)
    public User login (@RequestBody User user) {
        User login = userService.login(user);
        if(!Objects.isNull(login)){
            String token = RandomUtils.generateUuid();
            cookieOperations.set(COOKIE_TOKEN_NAME, token, USER_TOKEN_MAX_AGE);
            String redisUserKey = REDIS_USER_PREFIX + token;
            redisOperations.set(redisUserKey, user);
            redisOperations.expire(redisUserKey, USER_TOKEN_MAX_AGE);
        }
        return login;
    }

    /**
     * @Author:cxl
     * 获取当前登录用户
     * @return 登录成功的用户信息
     */
    @RequestMapping(value = "loginUser", method = RequestMethod.GET)
    public User getLoginUser(@CurUser User user) {

        return user;
    }


    /**
     * @Author:cxl
     * 退出登录操作
     * @return 是否登出成功
     */
    @RequestMapping(value = "logout", method = RequestMethod.POST)
    public boolean logout() {
        String token = cookieOperations.get(COOKIE_TOKEN_NAME);
        if (StringUtils.isBlank(token)) {
            throw new BusinessException(HttpStatus.UNAUTHORIZED, "用户已经退出登录");
        } else {
            //删除Cookie中对应的token
            cookieOperations.del(COOKIE_TOKEN_NAME);
            //删除token在redis中对应key
            redisOperations.del(REDIS_USER_PREFIX + token);
        }
        return true;
    }

    /**
     * @Author:cxl
     * 给手机发送短信验证码
     * @param phone 要发送短信的哪个手机号
     */
    @RequestMapping(value = "sendValidateCode", method = RequestMethod.POST)
    public boolean sendValidateCode(@RequestParam("phone") String phone) {
        //关于短信验证码的逻辑
        //1.首先通过点击“发送验证码”的按钮向指定的手机号发送验证码
        //2.因为调用的是第三方的短信平台，所以如果发送失败提示用户发送失败
        //如果发送成功，则提示用户发送成功，此时在服务器端，通过Redis以手机号作为key
        //将验证码作为value进行保存，同时为其设置过期时间
        //3.当取"注册"按钮的时候，将前端输入的验证码和Redis中的验证码去做对比，来确定验证码是否正确
        return userService.sendValidateCode(phone);
    }


}
